﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NPCScriptEditor.Windows;
using System.Dynamic;
using System.Threading;
using System.Net;
using System.Media;
using System.Diagnostics;

namespace NPCScriptEditor
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Thread tLoadAd = new Thread(new ThreadStart(LoadAd));
            tLoadAd.Start();
            Thread tLoadEng = new Thread(new ThreadStart(LoadEngIn));
            tLoadEng.Start();
            //FormEditor formEditor = new FormEditor();
            //formEditor.MdiParent = this;
            //formEditor.Show();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.Visible = false;
            AddPro();
        }

        private void AddPro()
        {
            Program.CurrentProj = "新的工程";
            MDIP formEditor = new MDIP(this,new string[0]);
           // formEditor.MdiParent = this;
            formEditor.Text = Program.CurrentProj;
            formEditor.Show();
            Program.path = "";
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            open();
            
        }

        private void open()
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Filter = "工程文件(*.ns)|*.ns|JS文件(*.js)|*.js|TXT文件(*.txt)|*.txt";
            var dg=openFile.ShowDialog();
            
            if (dg == DialogResult.OK)
            {
                Program.path = openFile.FileName;
            Program.CurrentProj =openFile.SafeFileName ;
                

                MDIP formEditor = new MDIP(this,   
                Classes.SaveLoad.load(Program.path));
            //formEditor.MdiParent = this;
            formEditor.Text = Program.CurrentProj;
            formEditor.Show();
                this.Visible = false;
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
        private void LoadEngIn() {
            this.BeginInvoke(new MethodInvoker(LoadEng));
        }
        private void LoadEng() {
            try {
            var a = Classes.http.Post("http://open.iciba.com/dsapi/");
            var b=LitJson.JsonMapper.ToObject(a);
            var EngContent = b["content"].ToString();
            var zhCNContent = b["note"].ToString();
            
            var w = new WebClient();
            w.DownloadFile(b["tts"].ToString(), Directory.GetCurrentDirectory() + "\\everyday.mp3");
            w.DownloadFile(b["picture2"].ToString(), Directory.GetCurrentDirectory() + "\\everyday.png");
            label_E.Text = EngContent + "\n" + zhCNContent;
                var img= Image.FromFile(Directory.GetCurrentDirectory() + "\\everyday.png");
                this.BackgroundImage = img;
                this.Width = img.Width;
                this.Height = img.Height;
                Debug.WriteLine(img.Width+" "+img.Height);
                Debug.WriteLine(panel1.Width +" "+img.Height);
                panel1.Location=new Point() {
                  X= (img.Width-panel1.Width)/2,
                   Y= (img.Height - panel1.Height) / 2
                };
                lExit.Location = new Point() { X=0,Y=this.Height-32};
                
            }
            catch(Exception exc)
            {
                label_E.Text = "就算没有每日一句，照样活得很开心\nEven if there is not an everyday-sentence,I am also happy!";
            }


        }
        private void LoadAd()
        {
            var a=Classes.http.Get("http://ns.heerdev.bid/ad.png");
            if (a != null)
            {
                pictureBox1.Image = a;
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            //FormCommunity fc = new FormCommunity();
            //fc.Show();
            System.Diagnostics.Process.Start("http://nsbbs.heerdev.bid");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            FormCommunity fc = new FormCommunity();
            fc.Show();
        }

        private void label_E_Click(object sender, EventArgs e)
        {
            //Mp3Player player = new Mp3Player(Directory.GetCurrentDirectory() + "\\everyday.mp3",Handle);
            // player.Open();
            使用API播放音乐.MP3Player mp3 = new 使用API播放音乐.MP3Player
            {
                FilePath = Directory.GetCurrentDirectory() + "\\everyday.mp3"
            };
            Debug.WriteLine(mp3.FilePath);
            mp3.Play();
        }

        private void lExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
